package com.txt.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.txt.annotion.Txt;
import com.txt.domain.User;
import com.txt.domain.UserLoginDTO;
import com.txt.result.R;
import com.txt.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/login")
    public R<String> login(@RequestBody UserLoginDTO userLoginDTO){
        String account = userLoginDTO.getAccount();
        String password = userLoginDTO.getPassword();
        //模拟登录校验
        if("chenxin".equals(account) && "123456".equals(password)){
            StpUtil.login(account);
            return R.ok("登陆成功");
        }
        return R.fail("登录失败");
    }


    @GetMapping("/islogin")
    public R<String> test(){
        return R.ok("当前会话是否登录: " + StpUtil.isLogin());
    }


    @GetMapping("/tokenvalue")
    @Operation(summary = "tokenValue")
    public R<String> tokenValue(){
        return R.ok(MessageFormat.format("tokenValue: {0}", StpUtil.getTokenValue()));
    }


    @GetMapping("/tokenInfo")
    @Operation(summary = "tokenInfo")
    public R<SaTokenInfo> tokenInfo(){
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return R.ok(tokenInfo);
    }



    @GetMapping("/tokenttk")
    @Operation(summary = "tokenTTK")
    public R<String> tokenTTK(){
        return R.ok(MessageFormat.format("tokenttk: {0}", StpUtil.getTokenTimeout()));
    }

    @PostMapping("logout")
    public R logout() {
        StpUtil.logout();
        return R.ok();
    }


    @Txt
    @GetMapping("/list")
    @Operation(summary = "用户列表")
    public R<List<User>> list() {
        List<User> list = userService.list();
        return R.ok(list);
    }

}
